//
//  CCYRecommendDetailTableViewController.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/9.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "CCYRecommendDetailTableViewController.h"
#import "CCYRecommendDetailView.h"
#import "AFNetworking.h"
#import "CCYMeSubscribModel.h"
#import "CCYMeNewTableViewCell.h"

@interface CCYRecommendDetailTableViewController ()
@property (nonatomic, strong) NSMutableArray *subArr;
@end

@implementation CCYRecommendDetailTableViewController

- (NSMutableArray *)subArr
{
    if (!_subArr) {
        _subArr = [[NSMutableArray alloc] init];
    }
    return _subArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CCYMeNewTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 10;
    [self createHeadView];
    [self analyCellData];
}

- (void)createHeadView
{
    CCYRecommendDetailView *detail = [CCYRecommendDetailView viewDidLoad];
    detail.height = 320;
    self.tableView.tableHeaderView = detail;
    [self analyHeadData];
}

- (void)analyHeadData
{
    NSString *str = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *dic = @{@"a" : @"profile", @"c" : @"user", @"userid" : _userId};
    [[AFHTTPSessionManager manager] GET:str parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.navigationItem.title = responseObject[@"data"][@"username"];
        [((CCYRecommendDetailView *)self.tableView.tableHeaderView) setMessageWithDic:responseObject[@"data"]];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XMGLog(@"解析title错误");
    }];
}

- (void)analyCellData
{
    NSString *str = [NSString stringWithFormat:@"http://s.budejie.com/topic/user-topic/%@/1/desc/bs0315-iphone-4.3/0-20.json", _userId];
    XMGLog(@"%@", str);
    [[AFHTTPSessionManager manager] GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in responseObject[@"list"]) {
            CCYMeSubscribModel *model = [[CCYMeSubscribModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.subArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XMGLog(@"解析cell错误");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.subArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CCYMeNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setVideoMessageWithModel:_subArr[indexPath.row]];
    return cell;
}

@end
